package com.shinechain.botModel;

import com.shinechain.TulingBot.Tuling;
import com.shinechain.dao.BotKitManMapper;
import com.shinechain.model.BotKitMan;
import com.shinechain.utils.PatternChat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.telegram.telegrambots.api.methods.groupadministration.KickChatMember;
import org.telegram.telegrambots.api.methods.send.SendMessage;
import org.telegram.telegrambots.api.methods.updatingmessages.DeleteMessage;
import org.telegram.telegrambots.api.objects.Message;
import org.telegram.telegrambots.api.objects.Update;
import org.telegram.telegrambots.bots.DefaultBotOptions;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.exceptions.TelegramApiException;

import java.io.IOException;


public class ShineChainProjectBot extends TelegramLongPollingBot {
    @Autowired
    PatternChat patternChat;

    @Override
    public void onUpdateReceived(Update update) {
        //chat
        if (update.hasMessage()&&!update.getMessage().isCommand()) {
            String reqMsg = update.getMessage().getText();
            System.out.println(reqMsg+update.getMessage().getChatId()+update.getMessage().getMessageId());
            boolean flag = false;
            try{
                flag = !update.getMessage().getNewChatMembers().isEmpty();
            }catch (Exception e){
                e.printStackTrace();
            }
            if (flag) {
                System.out.println("准备删除拉人信息");
                DeleteMessage deleteMessage = new DeleteMessage();
                deleteMessage.setChatId(update.getMessage().getChatId());
                deleteMessage.setMessageId(update.getMessage().getMessageId());
                try {
                    execute(deleteMessage); // Call method to send the message
                } catch (TelegramApiException e) {
                    e.printStackTrace();
                }
            }
            else if(update.getMessage().hasText()&&PatternChat.patternChat(reqMsg)!=null){
                SendMessage message = new SendMessage() // Create a SendMessage object with mandatory fields
                        .setChatId(update.getMessage().getChatId())
                        .setText(PatternChat.patternChat(reqMsg));
                try {
                    execute(message); // Call method to send the message
                } catch (TelegramApiException e) {
                    e.printStackTrace();
                }
            }
            //ad
            else if(update.getMessage().hasText()&&PatternChat.patternAd(reqMsg)){
                System.out.println("find AD by"+" chatId: "+update.getMessage().getFrom().getId()+" text:"+reqMsg);

                BotKitMan botKitMan = new BotKitMan();
                botKitMan.setAdmessage(reqMsg);
                botKitMan.setUserid(update.getMessage().getFrom().getId());
                botKitMan.setUsername(update.getMessage().getFrom().getUserName());
               //这个有问题 null
                // patternChat.insert(botKitMan);

                KickChatMember kickChatMember = new KickChatMember();
                kickChatMember.setChatId(update.getMessage().getChatId());
                kickChatMember.setUserId(update.getMessage().getFrom().getId());


                try {
                    execute(kickChatMember);
                } catch (TelegramApiException e) {
                    e.printStackTrace();
                }
            }
            else if(update.getMessage().hasText()){
                String resp;
                try {
                    resp = Tuling.chat(update.getMessage().getText());
                    SendMessage sendMessage = new SendMessage()
                            .setText(resp)
                            .setChatId(update.getMessage().getChatId());
                    execute(sendMessage);

                } catch (IOException e) {
                    e.printStackTrace();
                } catch (TelegramApiException e) {
                    e.printStackTrace();
                }
            }


        }




    }


    @Override
    public String getBotUsername() {
        return "ShineChainProject_bot";
    }

    @Override
    public String getBotToken() {
        return "662563102:AAEmDQUWqbVak-4yE-3k3gH3Cwxxe1eLGiA";
    }

    public ShineChainProjectBot(DefaultBotOptions options) {
        super(options);
    }
}
